'use strict';
angular.module('hanxinBroker').directive('fieldTip', function fieldTip($compile) {
  return {
    restrict: 'A',
    require: 'ngModel',
    link: function (scope, element, attrs, ngModel) {
      var subScope = scope.$new();
      var vm = subScope.vm = {};
      vm.tip = attrs.fieldTip;
      vm.visible = function () {
        return ngModel.$focused;
      };
      element.bind('focus', function () {
        subScope.$apply(function () {
          ngModel.$focused = true;
        });
      });
      element.bind('blur', function () {
        subScope.$apply(function () {
          ngModel.$focused = false;
        });
      });
      var $wrapper = element.parent();
      if (!$wrapper.hasClass('input-wrapper')) {
        $wrapper = element.wrap('<div class="input-wrapper"></div>').parent();
      }
      var $tip = $compile('<div class="floating label label-info" ng-if="vm.visible()">{{vm.tip}}</div>')(subScope);
      $wrapper.append($tip);
    }
  };
});
